package com.design.patterns.list.singleton;

/**
 * 懒汉式，同步代码块。无法解决线程不安全的问题
 * @author Wangzhiwen
 */
public class SingletonE {
}

class Singleton4{

    private Singleton4() {
    }

    private static Singleton4 singleton;

    /**
     * 线程不安全，只要两个线程都进入到了if语句内，同步了也无效。
     */
    public static Singleton4 getInstance(){
        if (singleton == null){
            synchronized (Singleton4.class){
                singleton = new Singleton4();
            }
        }
        return singleton;
    }
}

class Singleton5{

    private Singleton5() {
    }

    private static Singleton5 singleton;

    /**
     * 同步全部方法块，和同步整个方法没有区别。依然效率低
     */
    public static Singleton5 getInstance(){
        synchronized (Singleton5.class){
            if (singleton == null){
                singleton = new Singleton5();
            }
        }
        return singleton;
    }
}
